<?php
namespace App\Admin\Controllers;

use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Traits\HasUploadedFile;
use Illuminate\Http\Request;

class UploadController extends AdminController
{
    use HasUploadedFile;

    public function uploadProjectCover(Request $request)
    {
        $file = $this->file();

        $disk = $this->disk('public');

        if ($this->isDeleteRequest()) {
            return $this->deleteFileAndResponse($disk);
        }

        $dir = 'project';

        $column = md5($file->getClientOriginalName().time()).'.'.$file->getClientOriginalExtension();

        $result = $disk->putFileAs($dir, $file, $column);

        $path = "{$dir}/$column";

        return $result ? $this->responseUploaded($path, $disk->url($path)) : $this->responseErrorMessage('文件上传失败');
    }
}
